package com.zjuh.qss.biz.dao.ibatis;

import java.util.List;
import java.util.Map;

import wint.lang.utils.MapUtil;

import com.zjuh.qss.biz.dao.TopicDAO;
import com.zjuh.qss.biz.dao.query.TopicQuery;
import com.zjuh.qss.biz.domain.TopicDO;

public class TopicDAOIbatis extends BaseIbatisDAO implements TopicDAO {

	@Override
	public long create(TopicDO topic) {
		return (Long)this.getSqlMapClientTemplate().insert("TopicDAO.create", topic);
	}

	@Override
	public void delete(long id) {
		this.getSqlMapClientTemplate().update("TopicDAO.delete", id);
	}

	@Override
	public TopicDO queryById(long id) {
		return (TopicDO)this.getSqlMapClientTemplate().queryForObject("TopicDAO.queryById", id);
	}

	@Override
	public void update(TopicDO topic) {
		this.getSqlMapClientTemplate().update("TopicDAO.update", topic);
	}
	
	public void incrAttention(long id) {
		this.getSqlMapClientTemplate().update("TopicDAO.incrAttention", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TopicDO> queryForPage(TopicQuery topicQuery) {
		int count = (Integer)this.getSqlMapClientTemplate().queryForObject("TopicDAO.queryForPageCount", topicQuery);
		topicQuery.setTotalResultCount(count);
		return (List<TopicDO>)this.getSqlMapClientTemplate().queryForList("TopicDAO.queryForPage", topicQuery);
	}
	
	@SuppressWarnings("unchecked")
	public List<TopicDO> queryForPageOrderByAttention(TopicQuery topicQuery) {
		int count = (Integer)this.getSqlMapClientTemplate().queryForObject("TopicDAO.queryForPageCount", topicQuery);
		topicQuery.setTotalResultCount(count);
		return (List<TopicDO>)this.getSqlMapClientTemplate().queryForList("TopicDAO.queryForPageOrderByAttention", topicQuery);
	}

	@Override
	public void updateItemCount(long topicId, int itemCount) {
		Map<String, Object> params = MapUtil.newHashMap();
		params.put("topicId", topicId);
		params.put("itemCount", itemCount);
		this.getSqlMapClientTemplate().update("TopicDAO.updateItemCount", params);
	}

}
